package com.etung.iot.demo.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etung.iot.demo.dao.BoxMapper;
import com.etung.iot.demo.dao.ProjectMapper;
import com.etung.iot.demo.dao.UserMapper;
import com.etung.iot.demo.modles.BoxEntity;
import com.etung.iot.demo.modles.ProjectEntity;
import com.etung.iot.demo.modles.UserEntity;
import com.etung.iot.demo.services.ProjectService;
import com.etung.iot.demo.vo.req.ProjectReq;
import com.etung.iot.demo.vo.resp.ProjectRsp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BoxMapper boxMapper;


    @Transactional
    @Override
    public ProjectRsp saveProject(ProjectReq project) {
//        UserEntity userEntity = userMapper.selectById(project.getId());
//        if(userEntity == null){
//            throw new RuntimeException("当前用户不存在");
//        }
        ProjectEntity projectEntity = projectMapper.selectOne(new QueryWrapper<ProjectEntity>()
                .eq("user_id", project.getId())
                .eq("del_flag",0)
                .eq("name",project.getName()));

        if(projectEntity != null){
            throw new RuntimeException("当前项目已存在");
        }
        ProjectEntity newProject = new ProjectEntity();
        newProject.setUserId(project.getId()).setName(project.getName()).setDelFlag(0).setCreateTime(new Date());
        projectMapper.insert(newProject);
        ProjectRsp projectRsp = new ProjectRsp();
        projectRsp.setProjectId(newProject.getId());
        projectRsp.setName(newProject.getName());
        return projectRsp;
    }

    @Transactional
    @Override
    public void updateProject(ProjectReq project) {
        QueryWrapper<ProjectEntity> queryCondition = new QueryWrapper<>();
        queryCondition.eq("id",project.getProjectId());
        ProjectEntity projectEntity = projectMapper.selectOne(queryCondition);
        if (projectEntity!= null){
            projectEntity.setName(project.getName());
            projectMapper.updateById(projectEntity);
        }
    }

    @Transactional
    @Override
    public void delProject(ProjectReq project) {
        ProjectEntity projectEntity = projectMapper.selectOne(new QueryWrapper<ProjectEntity>().eq("id", project.getProjectId()));
        if (projectEntity!= null){
            projectEntity.setDelFlag(1);
            projectMapper.updateById(projectEntity);
        }
    }

    @Transactional
    @Override
    public List<ProjectEntity> queryProject(ProjectReq project) {
        QueryWrapper<ProjectEntity> queryCondition = new QueryWrapper<>();
        queryCondition.eq("user_id",project.getId());
        queryCondition.eq("del_flag",0);
        if (!StringUtils.isEmpty(project.getName())){
            queryCondition.like("name",project.getName());
        }
        return projectMapper.selectList(queryCondition);
    }

    @Override
    public List<BoxEntity> selectBoxList(ProjectReq project) {
        if (project.getProjectId() == null || project.getProjectId() == 0){
            return  projectMapper.selectBoxList(project.getId());
        }else{
            return boxMapper.selectList(new QueryWrapper<BoxEntity>()
                    .eq("project_id", project.getProjectId())
            );

        }
    }
}
